# -*- coding: utf-8 -*-
from odoo import models, fields, api, tools, _
from datetime import datetime
from odoo.exceptions import ValidationError, Warning

class TyibsMainContractWizardReject(models.TransientModel):
    """
    审核不通过对话框
    """
    _name = 'tyibs.main.contract.wizard.reject'
    content = fields.Text(required=True, size=300)

    @api.multi
    def reject(self,values):
        """
        审核不通过
        """
        self.ensure_one()
        res_model = values.get("active_model")
        res_id = values.get("active_id")
        self.env[res_model].browse(res_id).origin_write({'apply_status': 'reject',
            'check_log_ids': [(0, False, {
                'res_model': res_model,
                'res_id': res_id,
                'user_id': self.env.uid,
                'content': u"审核不通过，原因：%s" % self.content,
                'time': datetime.utcnow()
            })]
        })


class TyibsMainContractExtensionWizard(models.TransientModel):
    """
    续约
    """
    _name = 'tyibs.main.contract.wizard.extension'
    end_date = fields.Date(string=u"续约结束日期")

    @api.multi
    def contract_extension(self,values):
        """
        合同在执行时续约
        """
        self.ensure_one()
        res_model = values.get("active_model")
        res_id = values.get("active_id")
        parent_obj = self.env[res_model].browse(res_id)
        if self.end_date < parent_obj.end:
            raise Warning(u"续约结束日期必须在上次合同结束日期之后")
        parent_obj.origin_write({'end': self.end_date,
                                 'check_log_ids': [(0, False, {
                                     'res_model': res_model,
                                     'res_id': res_id,
                                     'user_id': self.env.uid,
                                     'content': u"续约，从%s续至%s" % (parent_obj.end, self.end_date),
                                     'time': datetime.utcnow()
                                 })]
                             })
